package com.axx.android.home.adapter;

import android.widget.ImageView;

import com.axx.android.LetterApp;
import com.axx.android.R;
import com.axx.android.home.model.SearchWrapper;
import com.axx.android.utils.ImageLoader;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sendtion.xrichtext.StringUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/8/5 0005.
 */

public class SearchAdapter extends BaseMultiItemQuickAdapter<SearchWrapper, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public SearchAdapter(List<SearchWrapper> data) {
        super(data);
        addItemType(SearchWrapper.HEADER, R.layout.view_item_search_title);
        addItemType(SearchWrapper.USER, R.layout.view_item_home_user);
        addItemType(SearchWrapper.LETTER, R.layout.view_item_search_letter);
    }

    @Override
    protected void convert(BaseViewHolder helper, SearchWrapper item) {
        if (item.getItemType() == SearchWrapper.HEADER) {
            helper.setText(R.id.tv_title, item.getTitle());
        }
        if (item.getItemType() == SearchWrapper.USER) {
            if (item.getUserListBean() != null) {
                helper.setText(R.id.tv_name, item.getUserListBean().getNickName());
                ImageLoader.loadRoundImage(item.getUserListBean().getHeadPic(), (ImageView) helper.getView(R.id.iv_header), mContext);
                helper.setText(R.id.tv_distance, item.getUserListBean().getDistance() + "km");
                helper.setImageResource(R.id.view_sex, "M".equals(item.getUserListBean().getSex()) ? R.drawable.ic_sex_man : R.drawable.ic_sex_woman);
            }

        }
        if (item.getItemType() == SearchWrapper.LETTER) {
            if (item.getLetterListBean().getFromUser() == null)
                return;
            if (item.getLetterListBean().getToUser() == null)
                return;
            helper.setText(R.id.tv_location, item.getLetterListBean().getFromPlace() + "至" + item.getLetterListBean().getToPlace());

            helper.setText(R.id.tv_from, item.getLetterListBean().getFromUser().getNickName());
            ImageLoader.loadRoundImage(item.getLetterListBean().getFromUser().getHeadPic(), (ImageView) helper.getView(R.id.iv_from_header), mContext);

            helper.setText(R.id.tv_to, item.getLetterListBean().getToUser().getNickName());
            ImageLoader.loadRoundImage(item.getLetterListBean().getToUser().getHeadPic(), (ImageView) helper.getView(R.id.iv_to_header), mContext);

            helper.setText(R.id.tv_no, "To" + item.getLetterListBean().getTrains() + "次");

            helper.addOnClickListener(R.id.iv_to_header);
            helper.addOnClickListener(R.id.iv_from_header);

            if ("0".equals(item.getLetterListBean().getStatus())) {
                helper.setText(R.id.tv_status, "正在路上");
            } else {
                helper.setText(R.id.tv_status, "已到达");
            }


            String title = item.getLetterListBean().getTitle();
            int length = title.length() < 30 ? title.length() : 30;
            title = title.substring(0, length);
            if (length == 30) {
                title = "#" + title + "...#" + getContentSize(item);
            } else {
                title = "#" + title + "#" + getContentSize(item);
            }

            helper.setVisible(R.id.tv_content, true);
            helper.setText(R.id.tv_content, title);


        }
    }

    private String getContentSize(SearchWrapper searchWrapper) {
        int size = 0;
        List<String> textList = StringUtils.cutStringByImgTag(searchWrapper.getLetterListBean().getText());
        for (String text : textList) {
            if (!text.contains("<img") && !text.contains("src=")) {
                text = text.trim();
                size = size + text.length();
            }
        }
        return size == 0 ? "" : size + "字";
    }

}
